package com.telecom.controller;

import com.alibaba.fastjson.JSONObject;
import com.dahuatech.hutool.core.bean.BeanUtil;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.hutool.json.JSONUtil;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.http.DefaultClient;
import com.dahuatech.icc.oauth.http.IClient;
import com.dahuatech.icc.oauth.http.IccHttpHttpRequest;
import com.dahuatech.icc.oauth.http.IccTokenResponse;
import com.dahuatech.icc.oauth.model.v202010.GeneralRequest;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;
import com.dahuatech.icc.oauth.profile.IccProfile;
import com.dahuatech.icc.util.SignUtil;
import com.telecom.common.HttpsUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ITest {

    public static void main(String[] args) throws ClientException {


        GeneralResponse generalResponse1 = HttpsUtils.iRequest("/evo-apigw/evo-oauth/1.0.0/oauth/public-key",Method.GET,null);
        Map result = (Map) JSONObject.parseObject(generalResponse1.getResult(),HashMap.class);
        Map data = (Map)result.get("data");
        String publicKey = data.get("publicKey").toString();

        //publicKey MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDGO/2OdLlC4Q17i5IBRdR4FFklJDYBUIut9cQloyLJ2fZXnhaMQ2tEZeoLXQOh0i9YiIW2NH3V0PqXY6WcnSqVbcc3qV7aqpkOOPkk3OF0nh6N/dDQpMK2oFyq3/66xznKuAA/jQeQI5sspG41qevE0GtqYX0kMdgtAmyXMHv7ZwIDAQAB
//        GeneralResponse generalResponse = HttpsUtils.iRequest("/evo-apigw/evo-oauth/1.0.0/oauth/public-key",Method.GET,null);
//        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnqXXul16TsXcSlLK7qvjW44I8sCqW6Gz1ABqp4Ms0FgW06z+64u2Rv+Es3xIuzVxzqjMDELnd6g67PxP1z58/w+AU1/p2BEhkS+9TY6m5BrG0kicqhDX+8zwtM4SxicmpmrP8hCheLHSsFOMo4V2SRKDnQ1bqbsQqiy6pEAUpCQIDAQAB";
        //MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnqXXul16TsXcSlLK7qvjW44I8sCqW6Gz1ABqp4Ms0FgW06z+64u2Rv+Es3xIuzVxzqjMDELnd6g67PxP1z58/w+AU1/p2BEhkS+9TY6m5BrG0kicqhDX+8zwtM4SxicmpmrP8hCheLHSsFOMo4V2SRKDnQ1bqbsQqiy6pEAUpCQIDAQAB
        Map<String,Object> map =  new HashMap<>();
        map.put("grant_type","password");
        map.put("username","工行区政府测试");
        map.put("password", SignUtil.encryptRSA("Admin123",publicKey));
        map.put("client_id","spark");
        map.put("client_secret","cfce62f1-e191-41aa-a83d-479c4760d50e");
        map.put("public_key",publicKey);
//        IccHttpHttpRequest pr = new IccHttpHttpRequest("https://140.249.211.40:7443/evo-apigw/evo-oauth/1.0.0/oauth/extend/token", Method.POST, JSONUtil.toJsonStr(map));
//        String prBody = pr.execute();
//        IccTokenResponse keyResp = (IccTokenResponse) BeanUtil.toBean(prBody, IccTokenResponse.class);
//        log.info("----结束执行----{}------返回报文:{}", "用户密码模式-认证申请",keyResp);
//        System.out.println(keyResp);

        IClient iClient = new DefaultClient();
        /**
         * 1、请求地址是统一网关入口，以 /evo-apigw 开头
         * 2、方法参见 @see com.dahuatech.hutool.http.Method
         */
        GeneralRequest generalRequest = new GeneralRequest("/evo-apigw/evo-oauth/1.0.0/oauth/extend/token", Method.POST);
        // set http post body
        generalRequest.setBody(JSONUtil.toJsonStr(map));
        // set http get method param 例如?name=xiao&mobile=1xxx
//        generalRequest.form("key", "value");
//        // set header
//        generalRequest.header("key", "value");
        // 发起请求处理应答
        GeneralResponse generalResponse =iClient.doAction(generalRequest, generalRequest.getResponseClass());
        System.out.println(generalResponse.getResult());

    }


}
